package org.lds.scheduled.job.admin.starter.holder;

import lombok.RequiredArgsConstructor;
import org.lds.scheduled.job.admin.business.job.model.request.JobBaseListPageRequest;
import org.lds.scheduled.job.admin.business.job.model.response.JobBaseResponse;
import org.lds.scheduled.job.admin.business.job.service.JobBaseService;
import org.lds.scheduled.job.common.model.dto.ResultDTO;
import org.lds.scheduled.job.common.model.db.PageResult;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class GetValidJobStarter extends AbstractGetValidInstanceStarter<JobBaseResponse> {
    private final JobBaseService jobBaseService;
    private final JobHolder jobHolder;

    @Override
    protected void save(JobBaseResponse instance) {
        jobHolder.save(instance.getId(), instance.getAppId());
    }

    @Override
    protected Long id(JobBaseResponse instance) {
        return instance.getId();
    }

    @Override
    protected ResultDTO<PageResult<JobBaseResponse>> pageInstances(Long id) {
        JobBaseListPageRequest request = new JobBaseListPageRequest();
        request.setEnable(true);
        request.setGtId(id);
        request.setPageNum(1);
        request.setPageSize(500);
        request.setOrderByKey("id");
        request.setOrderByDesc(false);
        return jobBaseService.listJobPage(request);
    }
}
